﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using JCNT.DAL;
using System.IO;
using Pub;
using ICSharpCode.TextEditor.Document;

namespace JCNT.NoteApp
{
    public partial class FTree : Form
    {
        CTreeII tree = new CTreeII();
        TreeNodeII nodeSelected;
        public string ID = "";
        public string sNoteID = "";
        public string sLabID = "";

        public FTree()
        {
            InitializeComponent();
        }

        private void FTree_Load(object sender, EventArgs e)
        {
            tViewDat.Visible = false;
            tViewLab.Dock = DockStyle.Fill;
            tViewDat.Dock = DockStyle.Fill;
            fill_Tree();    //按标签填充
            fill_Tree2();   //按日期填充
            fill_Item();    //生成右键菜单目录
        }

        /// <summary>
        /// 按标签和文章名称填充（只填充第一层）
        /// </summary>
        public void fill_Tree()
        {
            tViewLab.Nodes.Clear();
            TreeNodeII node = new TreeNodeII();
            node.Text = @"\";
            node.Tag = "0";
            node.ID = "0";
            node.RootID = "-1";
            node.sLab = @"\";
            node.sTit = "";
            node.ImageIndex = 0;
            node.SelectedImageIndex = 0;
            node.sHighlight = "";
            tree.fillTree_NextLevel(tViewLab, node);    //填充一级
            node.Expand();
            tree.findNode(tViewLab, ID);
        }

        /// <summary>
        /// 刷新节点下的项（动态加载子节点）
        /// </summary>
        public void ref_Tree()
        {
            if (tViewLab.SelectedNode != null)
            {
                TreeNodeII node = (TreeNodeII)tViewLab.SelectedNode;
                if (node.typ == TreeNodeII.TYP_LAB)
                {
                    node.Nodes.Clear();
                    tree.fillTree_NextLevel(null, node);
                    node.Expand();
                    //tree.findNode(tView1, ID);
                }

            }

            ref_Tree2();

            fill_Item();
        }

        /// <summary>
        /// 按日期填充目录
        /// </summary>
        public void fill_Tree2()
        {
            tViewDat.Nodes.Clear();
            TreeNodeII node = new TreeNodeII();
            node.Text = @"\";
            node.Tag = "0";
            node.ID = "0";
            node.RootID = "-1";
            node.sLab = @"\";
            node.sTit = "";
            node.ImageIndex = 0;
            node.SelectedImageIndex = 0;
            tree.fillTree2(tViewDat, node);
            node.Expand();
        }

        public void ref_Tree2()
        {
            tViewDat.Nodes.Clear();
            TreeNodeII node = new TreeNodeII();
            node.Text = @"\";
            node.Tag = "0";
            node.ID = "0";
            node.RootID = "-1";
            node.sLab = @"\";
            node.sTit = "";
            node.ImageIndex = 0;
            node.SelectedImageIndex = 0;
            tree.fillTree2(tViewDat, node);
            node.Expand();
        }

        /// <summary>
        /// 根据标签在右键菜单中生成目录树
        /// </summary>
        public void fill_Item()
        {
            btnMove.DropDownItems.Clear();

            ToolStripMenuItemII node = new ToolStripMenuItemII();
            node.Text = @"\";
            node.Tag = "0";
            node.ID = "0";
            node.RootID = "-1";
            node.sLab = @"\";
            node.sTit = "";
            node.ImageIndex = 0;
            tree.fillMenu(btnMove, node);

            foreach (ToolStripMenuItemII node2 in btnMove.DropDownItems)
            {
                node2.Click += btnItem_Click;
                regEvent_item_click(node2);
            }
        }

        /// <summary>
        /// 给右键菜单注册事件
        /// </summary>
        /// <param name="node1"></param>
        public void regEvent_item_click(ToolStripMenuItemII node1)
        {
            foreach (ToolStripMenuItemII node2 in node1.DropDownItems)
            {
                node2.Click += btnItem_Click;
                regEvent_item_click(node2);
            }
        }

        public void menu_Enable(Boolean b1)
        {
            btnLabAdd.Enabled = b1;
            btnLabEdt.Enabled = b1;
            btnLabDel.Enabled = b1;
        }

        /// <summary>
        /// 选中节点后显示下级内容或显示文章内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                TreeView tv1 = null;
                if (sender is TreeView)
                {
                    tv1 = (TreeView)sender;

                    string ID = tv1.SelectedNode.Tag.ToString();
                    //CPub.Send(ID);
                    this.ID = ID;
                    nodeSelected = (TreeNodeII)tv1.SelectedNode;
                    int i_typ = nodeSelected.typ;
                    if (i_typ == TreeNodeII.TYP_TIT)
                    {
                        //填充主窗口文本框
                        
                        Program.fmain.fillRchText(ID);
                        
                        int i_index = Program.fmain.tabCtrl_Txt.SelectedIndex;
                        Program.fnodeAdd.tboxCon.Text = Program.fmain.lstDoc[i_index].tboxCon.Text;
                        Program.fnodeAdd.RootID = nodeSelected.RootID;
                        Program.fnodeAdd.sLab = nodeSelected.sLab;
                        
                        sNoteID = nodeSelected.ID;
                        /*
                        string sTit = nodeSelected.Text.ToLower();
                        int i1 = sTit.Length;
                        //html用webBrow控件显示
                        if (sTit.Substring(i1 - 4, 4) == ".htm" || sTit.Substring(i1 - 5, 5) == ".html")
                        {
                            Program.fmain.webBrowCon.DocumentText = Program.fmain.tboxCon.Text;
                            Program.fmain.webBrowCon.Visible = true;
                            Program.fmain.tboxCon.Visible = false;
                        }
                        else
                        {
                            Program.fmain.tboxCon.Visible = true;
                            Program.fmain.webBrowCon.Visible = false;
                            //代码高亮显示
                            if (nodeSelected.sHighlight != null && nodeSelected.sHighlight != string.Empty)
                                Program.fmain.tboxCon.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy(nodeSelected.sHighlight);
                            Program.fmain.tboxCon.Encoding = System.Text.Encoding.Default;
                        }
                        */
                        menu_Enable(false);
                    }
                    else if (i_typ == TreeNodeII.TYP_LAB)
                    {
                        //加载子节点
                        if (nodeSelected.Nodes.Count == 0)
                            tree.fillTree_NextLevel(tv1, (TreeNodeII)tv1.SelectedNode);
                        Program.fnodeAdd.RootID = nodeSelected.ID;
                        Program.fnodeAdd.sLab = nodeSelected.sLab;
                        sLabID = nodeSelected.ID;

                        menu_Enable(true);
                    }
                }
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                CDebug.Send("点击树目录出错", ex);
            }


        }


        private void btnItem_Click(object sender, EventArgs e)
        {
            int i_btn = 0;
            if (sender is ToolStripMenuItem)
            {
                i_btn = Convert.ToInt16(((ToolStripMenuItem)sender).Tag);
                menuTree.Close();
            }

            if (tViewLab.SelectedNode != null)
            {
                int i_typ = nodeSelected.typ;
                if (i_typ == TreeNodeII.TYP_TIT)
                {
                    tree.noteMov(Convert.ToInt32(nodeSelected.ID), i_btn);
                }
                else if (i_typ == TreeNodeII.TYP_LAB)
                {
                    tree.labMov(Convert.ToInt32(nodeSelected.ID), i_btn);
                }
                tViewLab.SelectedNode.Remove();
                ref_Tree();
                //MessageBox.Show("" + node1.ID + "," + node1.RootID + "," + i_btn);
            }

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int i_btn = 0;
            if (sender is ToolStripMenuItem)
            {
                i_btn = Convert.ToInt16(((ToolStripMenuItem)sender).Tag);
            }

            switch (i_btn)
            {
                case 1:     //Add
                    Program.flabAdd.EDT_STATE = 1;
                    Program.flabAdd.showVal(nodeSelected);
                    Program.flabAdd.ShowDialog();
                    Program.fmain.Focus();
                    break;
                case 2:     //edit
                    Program.flabAdd.EDT_STATE = 2;
                    Program.flabAdd.showVal(nodeSelected);
                    Program.flabAdd.ShowDialog();
                    Program.fmain.Focus();
                    break;
                case 3:     //delete
                    if (CPub.MsgBox("确定要删除[" + nodeSelected.Text + "]吗？", "删除"))
                    {
                        tree.labDel(nodeSelected);
                        tViewLab.SelectedNode.Remove();
                    }
                    break;
                case 4:     //new
                    Program.fmain.btn_Click(3, null);
                    break;
                case 5:     //edit
                    Program.fmain.btn_Click(4, null);
                    break;
                case 6:
                    Program.fmain.btn_Click(5, null);
                    break;
                case 7:
                    Program.fimport.b_OK = false;
                    Program.fimport.timer1.Enabled = true;
                    Program.fimport.ShowDialog();
                    break;
                case 11:
                    ref_Tree();
                    break;
                case 100:

                    break;
            }
        }

        private void btnDir_Click(object sender, EventArgs e)
        {
            tViewLab.Visible = true;
            tViewDat.Visible = false;
        }

        private void btnDat_Click(object sender, EventArgs e)
        {
            tViewLab.Visible = false;
            tViewDat.Visible = true;
        }
    }
}
